import { SquareGroup } from "./SquareGroup"
import { IPoint, IShape } from "./types"
import { getRandom } from "./utils"

export class TShape extends SquareGroup {
    constructor(_centerPoint: IPoint, _color: string) {
        const shape: IShape = [
            { x: 0, y: -1 },
            { x: -1, y: 0 },
            { x: 0, y: 0 },
            { x: 1, y: 0 }
        ]
        super(shape, _centerPoint, _color)
    }
}

export class LShape extends SquareGroup {
    constructor(_centerPoint: IPoint, _color: string) {
        const shape: IShape = [
            { x: -2, y: 0 },
            { x: -1, y: 0 },
            { x: 0, y: 0 },
            { x: 0, y: -1 }
        ]
        super(shape, _centerPoint, _color)
    }
}

export class L2Shape extends SquareGroup {
    constructor(_centerPoint: IPoint, _color: string) {
        const shape: IShape = [
            { x: 2, y: 0 },
            { x: 1, y: 0 },
            { x: 0, y: 0 },
            { x: 0, y: -1 }
        ]
        super(shape, _centerPoint, _color)
    }
}

export class ZShape extends SquareGroup {
    constructor(_centerPoint: IPoint, _color: string) {
        const shape: IShape = [
            { x: 0, y: 0 },
            { x: 1, y: 0 },
            { x: 0, y: 1 },
            { x: -1, y: 1 }
        ]
        super(shape, _centerPoint, _color)
    }
    // 只有2种状态
    rotate() {
        super.rotate()
        this.isClock = !this.isClock
    }
}

export class Z2Shape extends SquareGroup {
    constructor(_centerPoint: IPoint, _color: string) {
        const shape: IShape = [
            { x: 0, y: 0 },
            { x: -1, y: 0 },
            { x: 0, y: 1 },
            { x: 1, y: 1 }
        ]
        super(shape, _centerPoint, _color)
    }
    // 只有2种状态
    rotate() {
        super.rotate()
        this.isClock = !this.isClock
    }
}

export class SquareShape extends SquareGroup {
    constructor(_centerPoint: IPoint, _color: string) {
        const shape: IShape = [
            { x: 0, y: 0 },
            { x: 1, y: 0 },
            { x: 0, y: 1 },
            { x: 1, y: 1 }
        ]
        super(shape, _centerPoint, _color)
    }
    afterRotateShape(): IShape {
        return this.shape
    }
}

export class LineShape extends SquareGroup {
    constructor(_centerPoint: IPoint, _color: string) {
        const shape: IShape = [
            { x: -1, y: 0 },
            { x: 0, y: 0 },
            { x: 1, y: 0 },
            { x: 2, y: 0 }
        ]
        super(shape, _centerPoint, _color)
    }
    // 只有2种状态
    rotate() {
        super.rotate()
        this.isClock = !this.isClock
    }
}

export class DShape extends SquareGroup {
    constructor(_centerPoint: IPoint, _color: string) {
        const shape: IShape = [{ x: 0, y: 0 }]
        super(shape, _centerPoint, _color)
    }
    afterRotateShape(): IShape {
        return this.shape
    }
}

// 形状数组：8种形状
export const shapes = [TShape, LShape, L2Shape, ZShape, Z2Shape, SquareShape, LineShape, DShape]
// 随机颜色
export const colors = ["red", "green", "blue", "yellow", "orange", "purple", "pink", "gray"]
/**
 * 函数：可以随机产生方块组合类对象 -> 颜色随机，形状随机
 */
export function createTeris(centerPoint: IPoint): SquareGroup {
    let randomShapeIndex = getRandom(0, shapes.length)
    const shape = shapes[randomShapeIndex]
    randomShapeIndex = getRandom(0, colors.length)
    const color = colors[randomShapeIndex]
    return new shape(centerPoint, color)
}
